package com.gz.servlet.servletcontext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/payServlet")
public class PayServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取ServletContext对象
        ServletContext servletContext = getServletContext();
        System.out.println("OrderServlet servletContext "+servletContext+
                " 运行类型: "+servletContext.getClass());

        //封装一下下面的方法
        int visit_count=WebUtils.visitcount(servletContext);
//        //1.从servletContext获取属性visit_count 属性 (相当于记录的Map的k-v数)
//        Object visit_count = servletContext.getAttribute("visit_count");
//        if(visit_count == null) {
//            //说明是第一次访问
//            servletContext.setAttribute("visit_count", 1);
//        }else{
//            int i = Integer.parseInt(visit_count.toString());
//            //访问一次就+1
//            i++;
//            servletContext.setAttribute("visit_count", i);
//        }
        resp.setContentType("text/html;charset=utf-8");

        PrintWriter out = resp.getWriter();
        out.println("<h1>网站被访问的次数是"+visit_count+"</h1>");
        out.flush();
        out.close();
    }
}
